import XEUtils from "xe-utils";
import VXETable from "vxe-table";
import common from './common';
import CustomInput from "../components/CustomInput";
import VxeCellLabel from "../components/VxeCellLabel";

VXETable.renderer.add("CustomInput", {
    autofocus: "input.el-input__inner",
    autoselect: true,
    renderCell(h, renderOpts, params) {
        const {row, column} = params;
        const {formatter} = renderOpts;
        let cellValue = XEUtils.get(row, column.property);
        if (typeof formatter === 'function') {
            cellValue = formatter(row, column);
        } else if (typeof column.params.formatter === 'function') {
            cellValue = column.params.formatter(row, column);
        }
        return [h(VxeCellLabel, {
            props: {
                node: cellValue,
            },
        })];
    },
    renderEdit(h, renderOpts, params) {
        const {row, column} = params;
        const {attrs, props, filter, formatter} = renderOpts;
        let cellValue = XEUtils.get(row, column.property);
        let check = typeof filter === 'undefined' || (typeof filter === 'function' && filter(row, column));
        if (!check) {
            if (typeof formatter === 'function') {
                cellValue = formatter(row, column);
            } else if (typeof column.params.formatter === 'function') {
                cellValue = column.params.formatter(row, column);
            }
            return [h(VxeCellLabel, {
                props: {
                    node: cellValue,
                },
            })];
        }
        return [
            h(CustomInput, {
                attrs: attrs,
                props: $.extend({}, props, {params, value: cellValue}),
                on: common.getEditOns(renderOpts, params),
                nativeOn: common.getNativeOns(renderOpts, params)
            })
        ];
    },
});
